package com.scoy.honeymei.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.scoy.honeymei.R;
import com.scoy.honeymei.utils.MyUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * ClassName:      com.oylib.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/31/0031$ 下午 15:57$
 * UpdateRemark:   更新内容
 */
public class ShareDialog extends DialogFragment {


    private TextView tvBtn0;
    private TextView tvBtn1;
    private TextView tvBtn2;
    private TextView tvBtn3;
    private TextView tvBtn4;
    private Context mContext;

    private String shareUrl;
    private String shareImage;
    private String shareTitle;
    private String shareContent;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            shareImage = getArguments().getString("shareImage");
            shareTitle = getArguments().getString("shareTitle");
            shareUrl = getArguments().getString("shareUrl");
            shareContent = getArguments().getString("shareContent");
        }
    }

    public static ShareDialog newInstance() {

        Bundle args = new Bundle();
        ShareDialog fragment = new ShareDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public static ShareDialog newInstance(String shareImage, String shareTitle, String shareContent, String shareUrl) {

        Bundle args = new Bundle();
        args.putString("shareImage", shareImage);
        args.putString("shareTitle", shareTitle);
        args.putString("shareContent", shareContent);
        args.putString("shareUrl", shareUrl);
        ShareDialog fragment = new ShareDialog();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.dialog_share, null, false);
        mContext = getContext();

        initView(view);


        return view;
    }

    private void initView(View view) {
        tvBtn0 = view.findViewById(R.id.dls_wechat_tv);
        tvBtn1 = view.findViewById(R.id.dls_pyq_tv);
        tvBtn2 = view.findViewById(R.id.dls_qq_tv);
        tvBtn3 = view.findViewById(R.id.dls_qqzom_tv);
        tvBtn4 = view.findViewById(R.id.dls_sina_tv);

        tvBtn0.setOnClickListener(v -> {
            dismiss();
            doShare(shareUrl, SHARE_MEDIA.WEIXIN);
        });
        tvBtn1.setOnClickListener(v -> {
            dismiss();
            doShare(shareUrl, SHARE_MEDIA.WEIXIN_CIRCLE);
        });
      /*  tvBtn2.setOnClickListener(v -> {
            dismiss();
            doShare(shareUrl, SHARE_MEDIA.QQ);
        });
        tvBtn3.setOnClickListener(v -> {
            dismiss();
            doShare(shareUrl, SHARE_MEDIA.QZONE);
        });
        tvBtn4.setOnClickListener(v -> {
            dismiss();
            doShare(shareUrl, SHARE_MEDIA.SINA);
        });*/

    }

    @Override
    public void onStart() {
        super.onStart();
        Window dialogWindow = getDialog().getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    private void doShare(String urlLink, SHARE_MEDIA platform) {
        UMWeb web = new UMWeb(urlLink);
        UMImage thumb = new UMImage(getActivity(), shareImage);
        web.setTitle(shareTitle);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(shareContent);//描述

        new ShareAction(getActivity())
                .setPlatform(platform)//传入平台
                .withMedia(web)
                .setCallback(shareListener)//回调监听器
                .share();
    }

    private UMShareListener shareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            MyUtil.mytoast0(mContext, "分享成功");
           /* if (onShareListen != null) {
                onShareListen.onSuccess();
            }*/
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            MyUtil.mytoast0(mContext, "失败" + t.getMessage());
           /* if (onShareListen != null) {
                onShareListen.onError();
            }*/
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            MyUtil.mytoast0(mContext, "分享取消");
           /* if (onShareListen != null) {
                onShareListen.onFail();
            }*/
        }
    };

    public interface OnShareListen {
        void onSuccess();

        void onError();

        void onFail();
    }

    OnShareListen onShareListen;

    public void setOnShareListen(OnShareListen onShareListen) {
        this.onShareListen = onShareListen;
    }

}
